import javax.crypto.Cipher;
import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * @ClassName RSAEncryptDecrypt
 * @Description TODO
 * @Author 赵雷棚
 * @Date 2024/6/26 14:42
 **/
// 加密与解密
public class RSAEncryptDecrypt {

    public static byte[] encrypt(PublicKey publicKey, String plainText) throws Exception {
        Cipher encryptCipher = Cipher.getInstance("RSA");
        encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return encryptCipher.doFinal(plainText.getBytes());
    }

    public static String decrypt(PrivateKey privateKey, byte[] encryptedBytes) throws Exception {
        Cipher decriptCipher = Cipher.getInstance("RSA");
        decriptCipher.init(Cipher.DECRYPT_MODE, privateKey);
        return new String(decriptCipher.doFinal(encryptedBytes));
    }

}
